#include"bmp.h"

int main(int argc, char const *argv[])
{
    int lcd = open("/dev/fb0", O_RDWR);//对文件io函数参数不够熟悉
    struct fb_var_screeninfo vinfo;
    ioctl(lcd, FBIOGET_VSCREENINFO, &vinfo);
    int lcd_w = vinfo.xres;
    int lcd_h = vinfo.yres;
    int components = vinfo.bits_per_pixel / 8;
    int screen_size = lcd_w * lcd_h * components;
    char *fb_mem = mmap(NULL, screen_size, PROT_WRITE, MAP_SHARED, lcd, 0);//忘记映射函数名mmap

    FILE *fp = fopen(argv[1], "rb");
    struct bitmap_header head;//对bmp结构体名字不熟
    struct bitmap_info info;//对bmp结构体名字不熟
    fread(&head, sizeof(head), 1, fp);
    fread(&info, sizeof(info), 1, fp);//依旧不熟悉参数，fread的第三个参数代表读取数据块的数量
    if(info.compression){
        struct rgb_quad quad;//不熟悉该结构体名
        fread(&quad, sizeof(quad), 1, fp);
    }
    //不熟悉接下来读bmp数据的流程
    int rgb_size = head.size - sizeof(head) - sizeof(info);
    char *rgb_data = malloc(rgb_size);
    fread(rgb_data, rgb_size, 1, fp);

    int pad = (4 - (info.width * info.bit_count/8) %4) %4;
    int lcd_offset = lcd_w * components;
    int rgb_offset = info.width * info.bit_count/8 + pad;
    for(int i = 0; i < lcd_h && i < info.height; i++){
        for(int j = 0; j < lcd_w && j < info.width; j++)
            memcpy(fb_mem + i*lcd_offset + j*components, 
                rgb_data + (info.height - 1 - i)*rgb_offset + j*info.bit_count/8, 3);//在这一段纠结了一下
    }

    free(rgb_data);
    fclose(fp);
    munmap(fb_mem, screen_size);
    close(lcd);
    return 0;
}
